 /**
  * 希尔排序-根据对应值排序
  * @param {Object} d 
  * @param {String} name 
  * @param {String} type desc降序大到小/asc升序小到大
  */
 function sortOrder(d, name, order) {
   var len = d.length,
     temp,
     gap = 1;
   while (gap < len / 5) {
     //动态定义间隔序列
     gap = gap * 5 + 1;
   }
   for (gap; gap > 0; gap = Math.floor(gap / 5)) {
     for (var i = gap; i < len; i++) {
       temp = d[i];
       for (var j = i - gap; j >= 0 && orderCompare(d[j][name], temp[name], order, name); j -= gap) {
         d[j + gap] = d[j];
       }
       d[j + gap] = temp;
     }
   }
   return d;
 }

 function orderCompare(p1, p2, type, name) {
   var res = null;
   if (isNaN(parseInt(p1)) || isNaN(parseInt(p2))) {
     res = type == 'asc' ? p1 > p2 : p1 < p2;
   } else {
     res = type == 'asc' ? parseInt(p1) > parseInt(p2) : parseInt(p1) < parseInt(p2);
   }
   return res;
 }
 /**
  * 格式化日期
  * @param {Date} date 
  */
 function formatTime(date) {
   const year = date.getFullYear()
   const month = date.getMonth() + 1
   const day = date.getDate()
   const hour = date.getHours()
   const minute = date.getMinutes()
   const second = date.getSeconds()
   return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
 }

 function formatNumber(n) {
   n = n.toString()
   return n[1] ? n : '0' + n
 }
 /**
  * 获取图片
  * @param {String} url 
  */
 function getImage(url) {
   return new Promise((resolve, reject) => {
     let img = new Image();
     // img.crossOrigin = 'Anonymous';
     img.src = url; //背景图片
     img.onload = (e) => {
       resolve(url);
     };
     img.onerror = () => {
       reject(false);
     };
   });
 }

 function getHtml(e, id) {
   let str = e.url.split('/');
   let url = str[2];
   let icon = str[0] + '//' + url + '/favicon.ico';
   let date = formatTime(new Date(e.lastVisitTime));
   getImage(icon).then(res => {
     $('#img-' + id).attr('src', res);
   }).catch(() => {
     $('#img-' + id).attr('src', '/images/default-icon.png');
   })
   if (!e.title) {
     if (e.url.indexOf('http://') > -1) {
       e.title = e.url.split('http://')[1];
     } else if (e.url.indexOf('https://') > -1) {
       e.title = e.url.split('https://')[1];
     }
   }
   let ift_col = $('<div>', {
     class: 'ift-col'
   });
   let app_container_item = $('<a>', {
     class: 'app-container_item',
     href: e.url
   });
   let app_container_icon = $('<div>', {
     class: 'app-container_icon'
   }).append('<img id="img-' + id + '" onerror="onIconError"/>');

   let app_container_info = $('<div>', {
     class: 'app-container_info'
   });
   let title = $('<p>', {
     class: 'app-container_name',
     html: e.title
   });
   let bottom = $('<div>', {
     class: 'bottom'
   }).append('<span>' + date + '  ' + e.visitCount + '</span>').append($('<span>', {
     class: 'url',
     html: url
   }));
   app_container_info.append(title).append(bottom);
   app_container_item.append(app_container_icon);
   app_container_item.append(app_container_info);
   ift_col.append(app_container_item);
   return ift_col;
 }
 //合并相同数据
 function mergeData(data, type) {
   let hash = [],
     arr = [];
   data.map((item) => {
     if (hash.indexOf(item[type]) == -1) {
       hash.push(item[type])
       arr.push(item)
     }
   })
   return arr;
 }